import { useCallback } from 'react';
import type { RouteObject } from 'react-router-dom';
import { Navigate, useRoutes } from 'react-router-dom';
import { useRecoilValue } from 'recoil';

import BWList from '@/pages/blackandWhiteList';
import DevicesConfig from '@/pages/devManage/devicesConfig';
import DeviceInfo from '@/pages/devManage/devicesInfo';
import DeviceStatus from '@/pages/devManage/deviceStatus';
import Homepage from '@/pages/HomePage';
import Init1 from '@/pages/init/SystemInit';
import KeyManagement from '@/pages/keyManage';
import Login from '@/pages/login';
import InterfaceLog from '@/pages/logManage/interfaceLog';
import ManageLog from '@/pages/logManage/manageLog';
import SystemLog from '@/pages/logManage/systemLog';
import NotFoundPage from '@/pages/NotFoundPage';
import System from '@/pages/system';
import UserManagement from '@/pages/userManage';
import { userType } from '@/store';

import AuthFirstVisit from './AuthFirstVisit';
import AuthRoute from './AuthRoute';
import type { routerConfigType } from './routerConfigType';

const routeConfig: routerConfigType[] = [
  {
    path: '',
    element: <Navigate to="login" replace></Navigate>,
  },
  {
    path: 'login',
    element: (
      <AuthFirstVisit>
        <Login />
      </AuthFirstVisit>
    ),
  },
  {
    path: '/init',
    element: <Init1 />,
  },
  {
    path: '/*',
    element: (
      <AuthRoute auth={[1, 2, 3]}>
        <Homepage />
      </AuthRoute>
    ),
    children: [
      {
        // 系统
        path: 'system',
        element: (
          <>
            <System></System>
          </>
        ),
      },
      {
        // 设备管理
        path: 'device/*',
        children: [
          {
            // 设备信息
            path: 'device-info',
            element: (
              <>
                <DeviceInfo></DeviceInfo>
              </>
            ),
          },
          {
            // 设备状态
            path: 'device-status',
            element: (
              <>
                <DeviceStatus></DeviceStatus>
              </>
            ),
          },
          {
            // 设备配置
            path: 'device-config',
            element: (
              <>
                <DevicesConfig></DevicesConfig>
              </>
            ),
          },
        ],
      },
      {
        // 密钥管理
        path: 'key-management',
        element: (
          <>
            <KeyManagement></KeyManagement>
          </>
        ),
      },
      {
        path: 'log/*',
        children: [
          {
            // 管理日志
            path: 'manage-log',
            element: (
              <>
                <ManageLog></ManageLog>
              </>
            ),
          },
          {
            // 系统日志
            path: 'system-log',
            element: (
              <>
                <SystemLog></SystemLog>
              </>
            ),
          },
          {
            // 接口日志
            path: 'interface-log',
            element: (
              <>
                <InterfaceLog></InterfaceLog>
              </>
            ),
          },
        ],
      },
      {
        // 用户管理
        path: 'user-management',
        element: (
          <>
            <UserManagement />
          </>
        ),
      },
      {
        // 访问控制
        path: 'bw-list',
        element: (
          <>
            <BWList />
          </>
        ),
      },
    ],
  },
  {
    path: '404',
    element: (
      <>
        <NotFoundPage />
      </>
    ),
  },
];

function MyRoutes() {
  const currentUserType = useRecoilValue(userType);
  /**
   * @description: 路由配置列表数据转换
   * @param {routeConfig} routeConfig 路由配置
   */
  const transformRoutes = useCallback(
    (routeList: typeof routeConfig) => {
      const list: RouteObject[] = [];
      routeList.forEach((route: routerConfigType) => {
        if (route.path === undefined) {
          return null;
        }
        if (
          route.path !== '404' &&
          route.auth !== undefined &&
          route.auth.find((item) => item === currentUserType) === undefined
        ) {
          route.element = <Navigate replace to="404"></Navigate>;
        }
        if (route.children) {
          route.children = transformRoutes(route.children);
        }

        list.push(route);
      });
      return list;
    },
    [currentUserType],
  );

  const getRoutes = useRoutes(transformRoutes(routeConfig));
  return <>{getRoutes}</>;
}
export default MyRoutes;
